package controller

import (
	"net/http"

	"github.com/go-playground/validator/v10"
	"github.com/kataras/iris"
	"github.com/kataras/iris/sessions"

	"im/common"
	"im/errcode"
	"im/service"
)

type Base struct {
	Ctx     iris.Context
	Session *sessions.Session
}

func (c *Base) SendSmile(data interface{}) {
	if common.IsNil(data){
		data = map[string]interface{}{}
	}
	c.Ctx.StatusCode(http.StatusOK)
	box := make(map[string]interface{})
	box["msg"] = "成功"
	box["code"] = 0
	box["data"] = data
	c.Ctx.JSON(box)
	return
}
func (c *Base) SendCry(err interface{}) {
	var code = errcode.UnexpectedError
	var msg = code.String()
	switch err.(type) {
	case validator.ValidationErrors:
		msg = service.Translate(err.(validator.ValidationErrors))
		code = errcode.RequestParamInvalid
	case errcode.Code:
		code = err.(errcode.Code)
		msg = code.String()
	}
	box := make(map[string]interface{})
	box["msg"] = msg
	box["code"] = code
	c.Ctx.StatusCode(http.StatusOK)
	_, _ = c.Ctx.JSON(box)
	return
}
